#include <iostream>
 #include<string>
 #include<unordered_map>
 #include<queue>
 using namespace std;
 
int main() {
  
    string s;
    while(getline(cin,s))
    {
          unordered_map<char,int> hash;
            for(auto e:s)
            {
                hash[e]++;
            }
            priority_queue<long long,vector<long long>,greater<long long>> heap;
            for(auto &[a,b]:hash)
            {
                heap.push(b);
            }
            if(heap.size()==1)
            {
                cout<<heap.top();
                return 0;
            }
            long long sum=0;
            while(heap.size()!=1)
            {
                long long t1=heap.top();
                heap.pop();
                long long t2=heap.top();
                heap.pop();
                sum+=t1+t2;
                heap.push(t1+t2);
            }
        
            cout<<sum<<endl;
 

    }


    return 0;
 
}
 // 64